import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.Scanner;
import java.io.*;
import java.util.Vector;

public class MyMainFrame extends JFrame {
	
	private void makeAutostart() {
			
		try {
			File dir = new File(System.getProperty("user.home")+"/.config/autostart");
			if (!dir.exists())
				dir.mkdir();
			File file = new File(System.getProperty("user.home")+"/.config/autostart/birthday-check.jar.desktop");
			PrintWriter out = new PrintWriter(file);
			out.println("");
			out.println("[Desktop Entry]");
			out.println("Type=Application");
			String jar = getClass().getProtectionDomain().getCodeSource().getLocation().getFile().replaceAll("%20", " ");
			File jarFile = new File(jar);
			out.println("Exec="+jarFile.getParent()+"/birthday-check.jar");
			out.println("Hidden=false");
			out.println("NoDisplay=false");
			out.println("X-GNOME-Autostart-enabled=true");
			out.println("Name[de_DE]=birthday-check");
			out.println("Name=birthday-check");
			out.println("Comment[de_DE]=");
			out.println("Comment=");
			out.close();
		}catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Unable to register the birthday calendar to the autostart.", "Error", JOptionPane.WARNING_MESSAGE);
		}
	}
	
	public MyMainFrame() {
		super("birthday calendar");
		JButton button;
		MyJTable table = loadBirthdays();
		table.setDefaultRenderer(Object.class, new MyRenderer());
		MyActionListener myActionListener = new MyActionListener(table);
		
		makeAutostart();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300,400);
		setVisible(true);
		Container content = getContentPane();
		content.setLayout(new BorderLayout());
		JPanel panel = new JPanel();
		panel.add(new JLabel("birtday calendar"));
		panel.add(new JLabel());
		content.add(panel, BorderLayout.NORTH);
		panel = new JPanel();
		content.add(table, BorderLayout.CENTER);	
		content.add(new JScrollPane((JTable)table));
		button = new JButton("delete");
		button.addActionListener(myActionListener);
		panel.add(button);
		button = new JButton("edit");
		button.addActionListener(myActionListener);
		panel.add(button);
		button = new JButton("add");
		button.addActionListener(myActionListener);
		panel.add(button);
		content.add(panel, BorderLayout.SOUTH);
		//pack();		
		
	}
	
	public MyJTable loadBirthdays() {
		Vector row;
		String s;
		MyTableModel model = new MyTableModel();
		int i; 
		
		model.setColumnIdentifiers(new Object[]{"name", "birthday"});
		
	
		try {
			String jar = getClass().getProtectionDomain().getCodeSource().getLocation().getFile().replaceAll("%20", " ");
			File jarFile = new File(jar);
			File file = new File(jarFile.getParent()+"/birthdays");
			if (!file.exists())
				file.createNewFile();
			Scanner iF = new Scanner( file );

			while (iF.hasNext()) {
				row = new Vector();		
				s = iF.nextLine();
				for (i = 0; i<s.length() && s.charAt(i) != '#'; i++);
				row.add(s.substring(1,i-2));
				row.add(s.substring(i+1, s.length()-1));
				model.addRow(row);
			}	
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Loading of table failed.", "Error", JOptionPane.WARNING_MESSAGE);
		}
		
		model.sort();
		
		return new MyJTable(model);
	}
	
	public static void main(String[] args) {
		MyMainFrame mf = new MyMainFrame();
	}
}
